package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.core.domain.entity.SysProperty;
import com.ruoyi.system.domain.SysHomeownerProperty;
import com.ruoyi.system.domain.SysPropertyContract;
import com.ruoyi.system.service.ISysHomeownerPropertyService;
import com.ruoyi.system.service.ISysPropertyContractService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysPropertyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房源信息Controller
 * 
 * @author ssd
 * @date 2024-03-25
 */
@RestController
@Validated
@RequestMapping("/system/property")
public class SysPropertyController extends BaseController
{
    @Autowired
    private ISysPropertyService sysPropertyService;
    @Autowired
    private ISysHomeownerPropertyService sysHomeownerPropertyService;
    @Autowired
    private ISysPropertyContractService sysPropertyContractService;

    /**
     * 查询房源信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:property:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProperty sysProperty)
    {
        startPage();
        List<SysProperty> list = sysPropertyService.selectSysPropertyList(sysProperty);
        return getDataTable(list);
    }

    /**
     * 导出房源信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:property:export')")
    @Log(title = "房源信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProperty sysProperty)
    {
        List<SysProperty> list = sysPropertyService.selectSysPropertyList(sysProperty);
        ExcelUtil<SysProperty> util = new ExcelUtil<SysProperty>(SysProperty.class);
        util.exportExcel(response, list, "房源信息数据");
    }

    /**
     * 获取房源信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:property:query')")
    @GetMapping(value = "/{propertyId}")
    public AjaxResult getInfo(@PathVariable("propertyId") Long propertyId)
    {
        return success(sysPropertyService.selectSysPropertyByPropertyId(propertyId));
    }

    /**
     * 新增房源信息
     */
    @PreAuthorize("@ss.hasPermi('system:property:add')")
    @Log(title = "房源信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SysProperty sysProperty) {
        Long userId = getUserId();
        String username = getUsername();
        // 插入房源信息
        sysProperty.setCreateBy(username);
        int insertedPropertyId = sysPropertyService.insertSysProperty(sysProperty);
        // 获取自动生成的 propertyId
        Long propertyId = sysProperty.getPropertyId();
        Long contractId = sysProperty.getContractId();
        // 更新房主房源id关联表
        SysHomeownerProperty sysHomeownerProperty = new SysHomeownerProperty();
        sysHomeownerProperty.setPropertyId(propertyId);
        sysHomeownerProperty.setHomeownerId(userId);
        sysHomeownerPropertyService.insertSysHomeownerProperty(sysHomeownerProperty);
        //更新房源合同id关联表
        SysPropertyContract sysPropertyContract = new SysPropertyContract();
        sysPropertyContract.setContractId(contractId);
        sysPropertyContract.setPropertyId(propertyId);
        sysPropertyContractService.insertSysPropertyContract(sysPropertyContract);
        return toAjax(insertedPropertyId);
    }


    /**
     * 修改房源信息
     */
    @PreAuthorize("@ss.hasPermi('system:property:edit')")
    @Log(title = "房源信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody SysProperty sysProperty)
    {
        return toAjax(sysPropertyService.updateSysProperty(sysProperty));
    }

    /**
     * 删除房源信息
     */
    @PreAuthorize("@ss.hasPermi('system:property:remove')")
    @Log(title = "房源信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{propertyIds}")
    public AjaxResult remove(@PathVariable Long[] propertyIds)
    {
        return toAjax(sysPropertyService.deleteSysPropertyByPropertyIds(propertyIds));
    }
}
